package com.hhq.demo.wechat.controller;

import com.hhq.demo.bean.*;
import com.hhq.demo.mvc.ResponseBody;
import com.hhq.demo.service.ExpressService;
import com.hhq.demo.service.impl.ExpressServiceImpl;
import com.hhq.demo.utils.DateFormatUtil;
import com.hhq.demo.utils.JSONUtil;
import com.hhq.demo.utils.Message;
import com.hhq.demo.utils.UserUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

/**
 * @author HHQ
 * @date 2021年12月18日
 */
public class ExpressController {

    ExpressService expressService = new ExpressServiceImpl();

    @ResponseBody("/wx/findExpressByUserPhone.do")
    public String findByUserPhone(HttpServletRequest request, HttpServletResponse response) {
        User wxUserInfo = UserUtil.getWxUserInfo(request.getSession());
        String phone = wxUserInfo.getPhone();
        List<Express> expressList = expressService.findByPhone(phone);
        ArrayList<ExpressList> returnList = new ArrayList<>();
        for (Express e : expressList) {
            String inTime = DateFormatUtil.format(e.getInTime());
            String outTime = e.getOutTime() == null ? "未出库" : DateFormatUtil.format(e.getOutTime());
            String status = e.getStatus() == 0 ? "待取件" : "已取件";
            String code = e.getCode() == null ? "已取件" : e.getCode();
            ExpressList e2 = new ExpressList(e.getId(), e.getNumber(), e.getCompany(), e.getReceiveName(), e.getReceivePhone(), e.getRecordPhone(), status, code, inTime, outTime);
            returnList.add(e2);
        }
        Message message = new Message();
        if (expressList.size() == 0) {
            message.setStatus(-1);
        } else {
            message.setStatus(0);
            Stream<ExpressList> status0Express = returnList.stream().filter(express -> {
                if ("待取件".equals(express.getStatus())) {
                    return true;
                } else {
                    return false;
                }
            }).sorted((o1, o2) -> {
                long o1Time = DateFormatUtil.toTime(o1.getInTime());
                long o2Time = DateFormatUtil.toTime(o2.getInTime());
                return (int) (o1Time - o2Time);
            });
            Stream<ExpressList> status1Express = returnList.stream().filter(express -> {
                return "已取件".equals(express.getStatus());
            }).sorted((o1, o2) -> {
                long o1Time = DateFormatUtil.toTime(o1.getInTime());
                long o2Time = DateFormatUtil.toTime(o2.getInTime());
                return (int) (o1Time - o2Time);
            });
            Object[] s0 = status0Express.toArray();
            Object[] s1 = status1Express.toArray();
            Map data = new HashMap<>();
            data.put("status0", s0);
            data.put("status1", s1);
            message.setData(data);
        }
        return JSONUtil.toJSON(message.getData());
    }

    @ResponseBody("/wechat/addExpress.do")
    public String addExpress(HttpServletRequest request, HttpServletResponse response) {
        User wxUserInfo = UserUtil.getWxUserInfo(request.getSession());
        String recordPhone = wxUserInfo.getPhone();
        String username = request.getParameter("username");
        String number = request.getParameter("number");
        String receivePhone = request.getParameter("phone");
        String company = request.getParameter("company");
        //String number, String company, String receiveName, String receivePhone, String recordPhone
        Express express = new Express(number, company, username, receivePhone, recordPhone);
        int insert = expressService.insert(express);
        Message message = new Message();
        if (insert > 0) {
            message.setStatus(1);
            message.setResult("录入成功");
        } else {
            message.setStatus(0);
            message.setResult("录入失败，请重新录入");
        }
        return JSONUtil.toJSON(message);
    }


    @ResponseBody("/wechat/findExpressByNumber.do")
    public String findExpressByNumber(HttpServletRequest request, HttpServletResponse response) {
        String number = request.getParameter("number");
        Express e = expressService.findByNumber(number);
        ExpressList e2 = null;
        if (e != null) {
            e2 = new ExpressList(e.getId(), e.getNumber(), e.getCompany(), e.getReceiveName(), e.getReceivePhone(), e.getRecordPhone(), e.getStatus() == 0 ? "确认取件" : "已取件", e.getCode(), DateFormatUtil.format(e.getInTime()), e.getOutTime() == null ? "未出库" : DateFormatUtil.format(e.getOutTime()));
        }
        Message msg = new Message();
        if (e == null) {
            msg.setStatus(0);
            msg.setResult("该快递不存在");
        } else {
            msg.setStatus(1);
            msg.setResult("查询成功");
            msg.setData(e2);
        }
        return JSONUtil.toJSON(msg);
    }

    @ResponseBody("/wechat/lazyRanking.do")
    public String lazyRanking(HttpServletRequest request, HttpServletResponse response) {
        String type = request.getParameter("type");
        Message message = new Message();
        if ("total".equals(type)) {
            List<RankingBean> result = expressService.rankExpressNumber();
            message.setStatus(1);
            message.setResult("查找成功");
            message.setData(result);
        } else if ("year".equals(type)) {
            List<RankingBean> result = expressService.rankExpressNumberToYear();
            message.setStatus(1);
            message.setResult("查找成功");
            message.setData(result);
        } else if ("month".equals(type)) {
            List<RankingBean> result = expressService.rankExpressNumberToMonth();
            message.setStatus(1);
            message.setResult("查找成功");
            message.setData(result);
        } else {
            message.setStatus(-1);
            message.setResult("排序方式错误");
        }
        return JSONUtil.toJSON(message);
    }

}
